/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.chinasofti.regie.weixin.service.impl;

import com.chinasofti.regie.weixin.service.api.MessageGateway;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.github.vioao.wechat.bean.request.SignatureInfo;
import com.github.vioao.wechat.bean.xml.in.InMessage;
import com.github.vioao.wechat.bean.xml.out.XmlMessage;
import com.github.vioao.wechat.support.TokenManager;
import com.github.vioao.wechat.utils.signature.DigestUtil;
import com.github.vioao.wechat.utils.signature.MsgCrypt;
import com.github.vioao.wechat.utils.signature.SignatureUtil;
import java.io.IOException;
import java.util.logging.Level;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author 张森
 */
public class WeixinMessageGateway implements MessageGateway {
    
    private static final Logger logger = LoggerFactory.getLogger(WeixinMessageGateway.class);
    
    public static final String AES = "aes";
    
    private String appid;
    
    private String appsercet;
    
    private String echoToken;
    
    private String encodingAesKey;

    @Override
    public boolean verify(String echostr) {
        return echostr!=null;
    }
    @Override
    public boolean verify(String encryptType, String echostr) {
        return isAES(encryptType)&&verify(echostr);
    }
    @Override
    public boolean verify(String signature,String timestamp, String nonce) {
        String token = TokenManager.getToken(appid);
        return SignatureUtil.check(signature, token, timestamp, nonce);
    }

    
    @Override
    public boolean verify(String encryptType, String signature, String msgSignature, String timestamp, String nonce) {
        String token = TokenManager.getToken(appid);
        if(isAES(encryptType)){
            SignatureUtil.check(msgSignature, token,timestamp,nonce);
        }
        return SignatureUtil.check(signature, token, timestamp, nonce);
    }
    
    @Override
    public String decode(String encryptType, String echostr) {
        if(isAES(encryptType)){
            return MsgCrypt.decrypt(appid, encodingAesKey, echostr);
        }else{
            return echostr;
        }
    }

    @Override
    public XmlMessage handle(String timestamp, String nonce, String encryptType, String msgSignature, String message) {
        String token = TokenManager.getToken(appid);
        String msg = message;
        if(isAES(encryptType)){
            msg = MsgCrypt.decryptMsg(appid, encodingAesKey, token, msgSignature, timestamp, nonce, message);
        }
        XmlMapper mapper = new XmlMapper();
        InMessage inMessage;
        try {
            inMessage = mapper.readValue(msg, InMessage.class);
        } catch (IOException ex) {
            logger.error("消息转换对象失败",ex);
        }
        
        return null;
    }

    @Override
    public String getJSApiTicket() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    private boolean isAES(String encryptType){
        return AES.equals(encryptType);
    }

}
